/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import BE.Artist;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Zalan
 */
public class ArtistDBManager {
     private final DBConnectionManager cm;
     
     private static ArtistDBManager instance = null;
     
     public static ArtistDBManager getInstance() throws IOException
     {
         if(instance == null)
         {
             instance = new ArtistDBManager();
         }
         return instance;
     }
     
     ArtistDBManager() throws IOException
     {
         cm = DBConnectionManager.getInstance();
     }
     
       private Artist getOneArtist(ResultSet rs) throws SQLException
    {
        int artistId = rs.getInt("ID");
        String artistName = rs.getString("Name");

        Artist art = new Artist(artistId, artistName);
        return art;

    }
       
       /**
     * @param artistId
     * @return
     */
    public Artist getById(int artistId) throws SQLException
    {
        try (Connection con = cm.getConnection())
        {
            String sql = "SELECT * FROM Artist WHERE ID = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, artistId);

            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                return getOneArtist(rs);
            }
        }
        return null;
    }

    /**
     *
     * @param artistName
     * @return
     * @throws SQLException
     */

    public Artist getByName(String artistName) throws SQLException
    {
        try (Connection con = cm.getConnection())
        {
            String sql = "SELECT * FROM Artist WHERE Name = ?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, artistName);

            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                return getOneArtist(rs);
            }
        }
        return null;
    }   
     
     
     
     
}
